package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.*;
import com.itheima.service.UserService;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private MenuDao menuDao;

    @Override
    public User findByusername(String username) {
        User user = userDao.findByusername(username);
        if (user == null) {
            return null;
        }
        Integer userid = user.getId();
        Set<Role> roles = roleDao.findByUserId(userid);
        if (roles.size() > 0) {
            for (Role role : roles) {
                Integer roleId = role.getId();
                Set<Permission> permissions = permissionDao.findByRoleid(roleId);
                role.setPermissions(permissions);
            }
        }
        user.setRoles(roles);
        return user;
    }

    @Override
    public List<Menu> findMenulist(String username) {
        User user = userDao.findByusername(username);
        Integer userid = user.getId();
        Set<Role> roles = roleDao.findByUserId(userid);
        if (roles.size() > 0) {
            for (Role role : roles) {
                Integer roleId = role.getId();
                Set<Menu> menus = menuDao.findMenus(roleId);
                List<Menu> list = new ArrayList<>();
                for (Menu menu : menus) {
                    Integer id = menu.getId();
                    List<Menu> menuList = menuDao.findByParentId(id);
                    menu.setChildren(menuList);
                    if (menu.getChildren().size() > 0) {
                        list.add(menu);
                    }
                }
                return list;
            }
        }
        return null;
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<User> page = userDao.findByQueryString(queryString);
        long total = page.getTotal();
        List<User> rows = page.getResult();
        return new PageResult(total, rows);
    }

    @Override
    public User findByid(Integer id) {
        return userDao.findByid(id);
    }

    @Override
    public Integer[] findroleids(Integer id) {
        return userDao.findroleids(id);
    }

    @Override
    public void add(User user, Integer[] roleids) {
        userDao.add(user);
        Integer id = user.getId();
        if (roleids.length > 0) {
            userDao.addroleids(id, roleids);
        }
    }

    @Override
    public void update(User user, Integer[] roleids) {
        userDao.update(user);
        Integer id = user.getId();
        if (roleids.length > 0) {
            userDao.deleteroleids(id);
            userDao.addroleids(id,roleids);
        }
    }

    @Override
    public void deleteByid(Integer id) {
        userDao.deleteroleids(id);
        userDao.deleteByid(id);
    }
}
